﻿// -----------------------------------------------------------------------------------------------
//  <copyright file="TfsTestTreeViewBuilder.cs" company="http://buginbox.wordpress.com/">
//    Copyright (c) GNU General Public License version 2 (GPLv2)
//  </copyright>
// <summary>
//    Copied/updated from http://blogs.microsoft.co.il/blogs/shair/archive/2010/07/06/tfs-api-part-27-test-plans-test-suites-test-cases-mapping.aspx Refer to articles index : http://blogs.microsoft.co.il/blogs/shair/archive/2011/05/18/tfs-api-index-parts-1-to-37.aspx
//    Cf. Shai Raiten from http://blogs.microsoft.co.il/blogs/shair/
// </summary>
// -----------------------------------------------------------------------------------------------

using System.Collections.Generic;
using System.Windows.Controls;

using Microsoft.TeamFoundation.TestManagement.Client;

namespace TestPlanMigration.Tree
{
    /// <summary>
    /// The test project migrator. 
    /// </summary>
    public class TfsTestTreeViewBuilder
    {
        /// <summary>
        /// The get test plans.
        /// </summary>
        /// <param name="testProject">The test project.</param>
        /// <param name="tree">The tree.</param>
        /// <returns>
        /// The buid tree view from test plans.
        /// </returns>
        public static bool BuidTreeViewFromTestPlans(TfsProjectTest testProject, System.Windows.Controls.TreeView tree)
        {
            var plans = testProject.AllPlans;

            if (plans == null)
            {
                return false;
            }

            var root = new TreeViewItem
            {
                Tag = TfsTestItemType.TeamProject,
                Header = TfsTestImageHelper.CreateHeader(testProject.TestProject.WitProject.Name, TfsTestItemType.TeamProject),                     
                IsExpanded = true
            };

            tree.Items.Add(root);

            foreach (ITestPlan plan in testProject.AllPlans)
            {
                var item = new TfsTestItem(plan);

                var planTree = new TreeViewItem
                {
                    Tag = item,
                    Header = TfsTestImageHelper.CreateHeader(plan.Name, TfsTestItemType.TestPlan),
                    IsExpanded = true
                };

                if (plan.RootSuite != null && plan.RootSuite.Entries.Count > 0)
                {
                    BuildTreeViewFromTestSuites(plan.RootSuite.Entries, planTree);
                }

                root.Items.Add(planTree);
            }

            return true;
        }

        /// <summary>
        /// The get plan suites.
        /// </summary>
        /// <param name="suites">The suites.</param>
        /// <param name="parentTreeItem">The tree_item.</param>
        private static void BuildTreeViewFromTestSuites(IEnumerable<ITestSuiteEntry> suites, TreeViewItem parentTreeItem)
        {
            foreach (ITestSuiteEntry suite_entry in suites)
            {
                var suite = suite_entry.TestSuite as IStaticTestSuite;

                if (suite != null)
                {
                    var item = new TfsTestItem(suite);

                    var suiteTree = new TreeViewItem
                    {
                        Tag = item,
                        Header = TfsTestImageHelper.CreateHeader(suite.Title, TfsTestItemType.TestSuite), 
                        IsExpanded = true 
                    };

                    BuildTreeViewFromTestCases(suite, suiteTree);

                    parentTreeItem.Items.Add(suiteTree);

                    if (suite.Entries.Count > 0)
                    {
                        BuildTreeViewFromTestSuites(suite.Entries, suiteTree);
                    }
                }
            }
        }

        /// <summary>
        /// The get test cases.
        /// </summary>
        /// <param name="suite">The suite.</param>
        /// <param name="parentTreeItem">The tree item.</param>
        private static void BuildTreeViewFromTestCases(IStaticTestSuite suite, TreeViewItem parentTreeItem)
        {
            // Will bring only the Test Case under a specific Test Suite.
            ITestSuiteEntryCollection suiteEntries = suite.TestCases;

            foreach (ITestSuiteEntry testcase in suiteEntries)
            {
                var item = new TfsTestItem(testcase);

                var test = new TreeViewItem
                {
                    Tag = item,
                    Header = TfsTestImageHelper.CreateHeader(testcase.Title, TfsTestItemType.TestCase), 
                    IsExpanded = true 
                };

                parentTreeItem.Items.Add(test);
            }
        }
    }
}